<?php
/***********************************************************
	Filename: libs/system/html.php
	Note	: 静态页生成类
	Version : 3.0
	Author  : qinggan
	Update  : 2010-2-15
***********************************************************/
class html_lib
{
	var $app;
	var $purl;
	var $timeout = 5;
	var $socket;
	function __construct()
	{
		$this->app = sys_init();
	}

	function html_lib()
	{
		$this->__construct();
	}

	function get_content($url)
	{
		if(!$url)
		{
			return false;
		}
		//格式化URL
		$this->format_url($url);
		$handle = @fsockopen($this->purl["host"], $this->purl['port'], $errno, $errstr, $this->timeout);
		if(!$handle)
		{
			return false;
		}
		stream_set_write_buffer($handle, 0);
		socket_set_timeout($handle, $this->timeout);
		//取得内容信息
		$urlext = $this->purl["path"];
		if($urlext != "/" && $this->purl["query"])
		{
			$urlext .= "?";
			$urlext .= $this->purl["query"];
			if($this->purl["fragment"])
			{
				$urlext .= "#".$this->purl["fragment"];
			}
		}
		$out = "GET ".$urlext." HTTP/1.1\r\n";
		$out.= "Host: ".$this->purl["host"]."\r\n";
		$out.= "Connection: Close\r\n";
		$out.= "Cookie: ".session_name()."=".session_id();
		$out.= "\r\n\r\n";
		fwrite($handle, $out);
		$content = "";
		while(!feof($handle))
		{
			$tmp = trim(fgets($handle));
			if($tmp)
			{
				$content .= $tmp."\n";
			}
			//$content .= fgets($handle);
		}
		fclose($handle);
		if($content)
		{
			$content = strstr($content,"<");
		}
		//清除备注信息
		if(!$content)
		{
			return false;
		}
		$content = str_replace("\r","",$content);
		return $content;
	}

	function format_url($url)
	{
		$this->purl = parse_url($url);
		if (!isset($this->purl['host']))
		{
			if(isset($_SERVER["HTTP_HOST"]))
			{
				$this->purl['host'] = $_SERVER["HTTP_HOST"];
			}
			elseif(isset($_SERVER["SERVER_NAME"]))
			{
				$this->purl['host'] = $_SERVER["SERVER_NAME"];
			}
			else
			{
				$this->purl['host'] = "localhost";
			}
		}
		if(!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] == "off" || $_SERVER["HTTPS"] == "")
		{
			$this->purl['scheme'] = "http";
		}
		else
		{
			$this->purl['scheme'] = "https";
		}
		$this->purl['port'] = $_SERVER["SERVER_PORT"] ? $_SERVER["SERVER_PORT"] : 80;
        if(!isset($this->purl['path']))
		{
			$this->purl['path'] = "/";
		}
		elseif(($this->purl['path']{0} != '/') && ($_SERVER["PHP_SELF"]{0} == '/'))
		{
			$this->purl['path'] = substr($_SERVER["PHP_SELF"], 0, strrpos($_SERVER["PHP_SELF"], '/') + 1) . $this->purl['path'];
		}
		return $this->purl;
	}
}
?>